package com.starshineworks.entities;

import com.starshineworks.util.FileUploader;

import com.db4o.ObjectContainer;
import com.starshineworks.util.DbUtil;
import com.starshineworks.util.Util;
import java.util.List;
import java.util.ArrayList;

public class FieldE extends EntityE {
    
    public String[] _agegroups;
    public String   _surface;
    public Integer  _comp;    // Should be boolean
    public Integer  _rec;     // Should be boolean
    public String[] _times;

    public FieldE() {
    }
    
    public FieldE(Integer id) {
        this._id = id;
    }

    /* m m m XXXX
    public FieldE(Integer id, String name, String surface, String[] times) {
        this._id      = id;
        this._name    = name;
        this._surface = surface;
        this._times   = times;
    }*/
    
    /* m m m m XXXX
    public static Integer create2(String name, String surface, String[] times) {
        // Create team with id and store it.
        ObjectContainer client = DbUtil.openClient();
        FieldE f = new FieldE(Util.nextId(), name, surface, times);
        client.store(f);
        client.close();
        return f._id;
    }*/

    //private static Map<String,Fieldx> allFields_ = null;
    private static boolean init_ = false;

    public static boolean init() {
        if (!init_) {

            String fn = Util.userHome + "/.fieldini";
            FileUploader.importFromFile(fn, "FieldE");
            init_ = true;
            return true;
        }
        return false;
    } 
    
    @Override
    public String refreshDisplay(int hview)
    {
        _display = "XXXX Name is " + _name;
        if (hview != Util.HTMLViewNone) {
            ArrayList<String> keys = new ArrayList<String>();
            keys.add("Field name");
            keys.add("ID");
            keys.add("Surface");
            keys.add("Time slots");
            keys.add("Comp/Rec");
            keys.add("Age groups");
            ArrayList<String> vals = new ArrayList<String>();
            vals.add(_name);
            vals.add(_id.toString());
            vals.add(_surface);
            // Time slots
            String slots = "";
            for (int i = 0; i < _times.length; i++) {
                if (i > 0) {
                    slots += " ";
                }
                slots += _times[i];
            }
            vals.add(slots);
            // Comp/Rec
            String compRec = "";
            if (_comp != 0) {
                compRec += "Comp ";
            }
            if (_rec != 0) {
                compRec += "Rec";
            }
            vals.add(compRec);
            // Agegroups
            String groups = "";
            for (int i = 0; i < _agegroups.length; i++) {
                if (i > 0) {
                    groups += " ";
                }
                groups += _agegroups[i];
            }
            vals.add(groups);
            if (hview == Util.HTMLViewTable) {
                List<List<String>> t = new ArrayList<List<String>>();
                t.add(vals);
                _display = Util.asHTMLTable(keys, t);
                return _display;
            }
        }
        return _display;
    }
}


